<?php

require_once('../../connection/_dbconnect_.php');
require_once('m_activity_log.php');

function getUsuarios(){
	$parentArray = array();

	$sql = "select u.iduser,u.fullname,u.enabled,u.idrole,r.name as role_name from user u\n
			join role r on (u.idrole = r.idrole)\n
			order by u.enabled DESC, u.iduser ASC";
	
	//echo $sql;exit;
	
	$result = mysql_query($sql) or die("Query: $sql \n\n".mysql_error());
	while($obj = mysql_fetch_object($result)){
		$childArray = array();		
		$childArray[] = $obj->iduser;
		$childArray[] = $obj->fullname;
		$childArray[] = $obj->enabled;
		$childArray[] = $obj->idrole;		
		$childArray[] = $obj->role_name;		
		$parentArray[]= $childArray;
	}	
	return $parentArray;
}

// function to get only the usernames of all users in the system
function getUsernames(){
	$parentArray = array();

	$sql = "select iduser from user";
	$result = mysql_query($sql) or die("Query: $sql \n\n".mysql_error());
	while($obj = mysql_fetch_object($result)){
		$parentArray[] = $obj->iduser;
	}	
	return $parentArray;
}

function getUser($id){

	$fetchedUser = array();
	
	$sql = "select iduser,fullname,password,enabled,idrole from user where iduser='".$id."'";
	
	$result = mysql_query($sql) or die("Query: $sql \n\n".mysql_error());
	while($obj = mysql_fetch_object($result)){
		$fetchedUser[] = $obj->iduser;
		$fetchedUser[] = $obj->fullname;
		$fetchedUser[] = $obj->password;
		$fetchedUser[] = $obj->enabled;
		$fetchedUser[] = $obj->idrole;
	}

	return $fetchedUser;
}

function addUser($addiduser,
				 $addfullname,
				 $addpassword,
				 $addenabled,
				 $addidrole){	
	$sql = "insert into user values ('$addiduser','$addfullname','$addpassword','$addenabled','$addidrole');";	
	$result = mysql_query($sql) or die("Query: $sql \n\n".mysql_error());
	
	if (!$result) {
		return false;
	} else {
		$date =new DateTime('NOW');		
		$result=addLogEntry($date->format('Y-m-d'), "User=".$saveiduser." was created");	
		return true;
	}
}

function saveUserEdit($saveiduser,
				 $savefullname,
				 $savepassword,
				 $saveenabled,
				 $saveidrole){
	
	$sql = "update user set fullname='".$savefullname."', 
							password='".$savepassword."',  
							enabled='".$saveenabled."', 
							idrole='".$saveidrole."' 
							where iduser='".$saveiduser."';";
	
	$result = mysql_query($sql) or die("Query: $sql \n\n".mysql_error());
	
	if (!$result) {
		return false;
	} else {
		$date =new DateTime('NOW');		
		$result=addLogEntry($date->format('Y-m-d'), "User=".$saveiduser." was edited");
		
		return true;
	}
}

function getRoles(){	
	$parentArray = array();	
	$sql = "select idrole,name from role";	
	$result = mysql_query($sql) or die("Query: $sql \n\n".mysql_error());
	while($obj = mysql_fetch_object($result)){
		$childArray = array();		
		$childArray[] = $obj->idrole;
		$childArray[] = $obj->name;		
		$parentArray[]= $childArray;
	}
	return $parentArray;
}


?>